package com.sbtr.controller.classify;

import com.sbtr.business.classify.entity.AlgoParamVersion;
import com.sbtr.business.classify.entity.AlgoTemplate;
import com.sbtr.business.classify.entity.AlgoTemplateParam;
import com.sbtr.business.classify.service.AlgoTemplateParamService;
import com.sbtr.common.LoginConstants;
import com.sbtr.common.Response;
import com.sbtr.util.BeanCopyUtils;
import com.sbtr.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.sbtr.common.PageDto;
import com.sbtr.business.classify.dto.AlgoParamVersionDto;
import com.sbtr.business.classify.service.AlgoParamVersionService;
import org.springframework.web.bind.annotation.RestController;
import com.sbtr.controller.common.CommonController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;

/**
 * 轻量化算法参数版本(高质量芯片项目) 前端控制器
 *
 * @author maojianping
 * @since 2023-04-17
 */
@RestController
@RequestMapping("/algoParamVersion")
@ApiIgnore
public class AlgoParamVersionController extends CommonController<AlgoParamVersion,AlgoParamVersionService> {

    @Autowired
    AlgoTemplateParamService algoTemplateParamService;

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "分页列表")
    public Response<PageDto<AlgoParamVersion>> pageList(AlgoParamVersionDto algoParamVersionDto){
        return Response.success(u.pageList(algoParamVersionDto));
    }

    @PostMapping("/addVersionByOwner")
    @ApiOperation(httpMethod = "POST", value = "算法文件上传者使用，一般是系统未正确读取文件时候需要手动创建默认版本")
    public Response<AlgoParamVersion> addVersionByOwner(@RequestBody AlgoParamVersion algoParamVersion){
        algoParamVersion.setId(null);
        if(algoParamVersion.insert()){
            return Response.success(algoParamVersion);
        }else throw new BDException("新增算法文件默认版本失败");
    }

    @PostMapping("/copyNewVersion")
    @ApiOperation(httpMethod = "POST", value = "在默认版本基础上，copy新版本出来，实现同一个算法文件的多版本动态管理")
    public Response<AlgoParamVersion> copyNewVersion(@RequestBody AlgoParamVersion algoVersion){

        String username = HttpServletRequestUtil.getLoginUserName();
        // 1.基于默认版本Id.查询其关联的算法参数
        List<AlgoTemplateParam> paramList = algoTemplateParamService.queryList(algoVersion.getId());
        algoVersion = u.copyNewVersion(username, algoVersion, paramList);
        return Response.success(algoVersion);
    }

    @ApiOperation(value = "根据id删除", notes = "根据id删除算法参数版本及 算法版本关联参数集）", httpMethod = "DELETE")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
    public Response<Boolean> deleteById(@PathVariable("id") Long id) {

        // 删除当前算法版本、算法版本关联参数集
        boolean b = u.removeById(id);
        if (!b)
            throw new BDException("删除失败");

        return Response.success("删除成功", b);
    }

}

